package zkclient;

import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;

import java.util.List;

public class Get_Data_Sample {
    public static void main(String[] args) throws InterruptedException {
        String path = "/lg-rpc-provider";
        ZkClient zkClient = new ZkClient("192.168.200.20:2181");
        //判断节点是否存在
        boolean exists = zkClient.exists(path);
        if (!exists) {
            zkClient.createPersistent(path, "123");
        }

        //注册监听
        zkClient.subscribeDataChanges(path, new IZkDataListener() {
            public void handleDataChange(String path, Object data) throws
                    Exception {
                System.out.println(path + "该节点内容被更新，更新后的内容" + data);
            }

            public void handleDataDeleted(String s) throws Exception {
                System.out.println(s + " 该节点被删除");
            }
        });
        zkClient.createEphemeral(path + "/192.168.200.20:8080");
        List<String> children = zkClient.getChildren("/lg-rpc-provider");
        for (String child : children) {
            System.out.println("/lg-rpc-provider的子节点" + child);
        }
        //获取节点内容
        Object o = zkClient.readData(path);
        System.out.println(o);
        //更新
        zkClient.writeData(path, "4567");
        Thread.sleep(1000);
        //删除
//        zkClient.delete(path);
//        Thread.sleep(1000);
    }
}
